//
//  TacticsViewController.m
//  Rong360
//
//  Created by tianliwei on 6/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "TacticsViewController.h"
#import "GridCellItem.h"
#import "AboutViewController.h"
#import "WebViewController.h"
#import "MFLCViewController.h"
#import "FWFSViewController.h"
#import "KFXFViewController.h"
#import "FCJGViewController.h"
#import "ZJZXContentViewController.h"
#import "ZJZXMailViewController.h"
#define GridViewColumn 3.0
#define GridViewRow    3.0
#define GridViewMarginX 0
#define GridViewMarginY 0
#define GridViewCellWidth (UI_SCREEN_WIDTH/GridViewColumn)
#define GridViewCellHeight ((UI_NAVIFRAME_HEIGHT-UI_TAB_BAR_HEIGHT)/GridViewRow)

@interface TacticsViewController ()
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIGridView *gridTable;
@property (nonatomic, strong) UIImageView *indicatorView;
@end
@implementation TacticsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"买房攻略" image:nil tag:0];
        [[self tabBarItem] setFinishedSelectedImage:[UIImage imageNamed:@"tab_2_focus"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_2"]];
        [[self tabBarItem] setTitleTextAttributes:@{UITextAttributeTextColor: [UIColor grayColor],
                                                    UITextAttributeFont:[UIFont systemFontOfSize:kTabBarFontSize]
                                                
                                                    } forState:UIControlStateNormal];
        [[self tabBarItem] setTitleTextAttributes:@{UITextAttributeTextColor: RGBCOLOR(0, 172, 246),
                                                    UITextAttributeFont:[UIFont systemFontOfSize:kTabBarFontSize]
                                                    } forState:UIControlStateSelected];
        [[NSNotificationCenter defaultCenter] addObserver:self
												 selector:@selector(reloadGridView)
													 name:kNewMsgNotification
												   object:nil];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.gridTable reloadData];
}

- (void)reloadGridView{
    [self.gridTable reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.title = @"买房助手";
    self.dataArray = [NSMutableArray arrayWithObjects:
                      [GridCellItem itemWithCellTitle:@"买房流程" andImageName:@"grid_1"],
                      [GridCellItem itemWithCellTitle:@"房屋风水" andImageName:@"grid_2"],
                      [GridCellItem itemWithCellTitle:@"挑选中介" andImageName:@"grid_3"],
                      [GridCellItem itemWithCellTitle:@"看房选房" andImageName:@"grid_4"],
                      [GridCellItem itemWithCellTitle:@"买房税费" andImageName:@"grid_5"],
                      [GridCellItem itemWithCellTitle:@"公积金贷款" andImageName:@"grid_6"],
                      [GridCellItem itemWithCellTitle:@"商贷办理" andImageName:@"grid_7"],
                      [GridCellItem itemWithCellTitle:@"房产交割" andImageName:@"grid_8"],
                      [GridCellItem itemWithCellTitle:@"专家咨询" andImageName:@"grid_9"],
                      nil];
    self.gridTable = [[UIGridView alloc]initWithFrame:CGRectMake(GridViewMarginX, 0, UI_SCREEN_WIDTH - 2 * GridViewMarginX, UI_NAVIFRAME_HEIGHT)];
    self.gridTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.gridTable.uiGridViewDelegate = self;
    
    UIButton *aboutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aboutButton setBackgroundColor:[UIColor clearColor]];
    [aboutButton setTitle:@"关于" forState:UIControlStateNormal];
    [aboutButton addTarget:self action:@selector(about) forControlEvents:UIControlEventTouchUpInside];
    aboutButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    aboutButton.frame = CGRectMake(0.0f, 0.0f, 40.0f, 25.0f);
    UIBarButtonItem *aboutButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aboutButton];
    self.navigationItem.rightBarButtonItem = aboutButtonItem;
    
    [self.view addSubview:self.gridTable];
}


- (void)about{
    self.hidesBottomBarWhenPushed = YES;
    AboutViewController *aboutVC = [[AboutViewController alloc]init];
    [self.navigationController pushViewController:aboutVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

- (CGFloat) gridView:(UIGridView *)grid widthForColumnAt:(int)columnIndex
{
	return GridViewCellWidth;
}

- (CGFloat) gridView:(UIGridView *)grid heightForRowAt:(int)rowIndex
{
	return GridViewCellHeight;
}

- (NSInteger) numberOfColumnsOfGridView:(UIGridView *) grid
{
	return GridViewColumn;
}


- (NSInteger) numberOfCellsOfGridView:(UIGridView *) grid
{
	return self.dataArray.count;
}


- (UIGridViewCell *) gridView:(UIGridView *)grid cellForRowAt:(int)rowIndex AndColumnAt:(int)columnIndex
{
	UIGridViewCell *cell = [grid dequeueReusableCell];
	
	if (cell == nil) {
		cell = [[UIGridViewCell alloc]init];
        self.indicatorView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"indicator"]];
        self.indicatorView.frame = CGRectMake(GridViewCellWidth-30, 40, 10, 10);
        [cell addSubview:self.indicatorView];
	}
    GridCellItem *item = [self.dataArray objectAtIndex:rowIndex * GridViewColumn + columnIndex];
    [cell setBackgroundColor:[UIColor whiteColor] imageName:item.imageName title:item.title];
    //收到新答案，添加右上角的红点
    self.indicatorView.hidden = YES;
    if (rowIndex * GridViewColumn + columnIndex == 8) {
        NSArray *messageArray = [[DBMan shareDB] getAllSectionQuesIDInQAInfo];
        for (int i = 0; i < messageArray.count; i++) {
            NSArray *arrayInSameQuesID = [[DBMan shareDB] getAllReplysByQuestionID:messageArray[i]];
            for (int j = 0; j < arrayInSameQuesID.count; j++) {
                MsgInfo *msgInfo = arrayInSameQuesID[j];
                if (msgInfo.isReply == 1 && msgInfo.isRead == 0) {
                    self.indicatorView.hidden = NO;
                    break;//有符合条件的就终止判断
                }else{
                    self.indicatorView.hidden = YES;
                }
            }
        }
    }
    return cell;
}

- (void) gridView:(UIGridView *)grid didSelectRowAt:(int)rowIndex AndColumnAt:(int)colIndex
{
    NSInteger index = rowIndex * GridViewColumn +colIndex;
    self.hidesBottomBarWhenPushed = YES;
    switch (index) {
        case 0:{
            MFLCViewController *mflcVC = [[MFLCViewController alloc]init];
            [self.navigationController pushViewController:mflcVC animated:YES];
            break;
        }
        case 1:{
            FWFSViewController *fwfsVC = [[FWFSViewController alloc]init];
            [self.navigationController pushViewController:fwfsVC animated:YES];
            break;
        }
            
        case 2:{
            WebViewController *webVC = [[WebViewController alloc]initWithUrl:TXZJ title:@"挑选中介" desc:TXZJDesc];
            webVC.type = HTML5ShareUrl;
            [self.navigationController pushViewController:webVC animated:YES];
            break;
        }

            
        case 3:{
            KFXFViewController *kfxfVC = [[KFXFViewController alloc]init];
            [self.navigationController pushViewController:kfxfVC animated:YES];
            break;
        }
        case 4:{
            WebViewController *webVC = [[WebViewController alloc]initWithUrl:MFSFUrl title:@"买房税费" desc:MFSFDesc];
            webVC.type = HTML5ShareUrl;
            [self.navigationController pushViewController:webVC animated:YES];
            break;
        }
           
        case 5:{
            WebViewController *webVC = [[WebViewController alloc]initWithUrl:GJJDKUrl title:@"公积金贷款" desc:GJJDKDesc];
            webVC.type = HTML5ShareUrl;
            [self.navigationController pushViewController:webVC animated:YES];
            break;
        }            
        case 6:{
            WebViewController *webVC = [[WebViewController alloc]initWithUrl:SDBL title:@"商贷办理" desc:SDBLDesc];
            webVC.type = HTML5ShareUrl;
            [self.navigationController pushViewController:webVC animated:YES];
            break;
        }
        case 7:{
            FCJGViewController *fcjgVC = [[FCJGViewController alloc]init];
            [self.navigationController pushViewController:fcjgVC animated:YES];
            break;
        }
        case 8:{
            if (![[NSUserDefaults standardUserDefaults]objectForKey:kCurrentCityID]) {
                CityListViewController *cityVC = [[CityListViewController alloc]init];
                cityVC.dismissStyle = Mail;
                cityVC.delegate = self;
                UINavigationController *naviVC = [[UINavigationController alloc]initWithRootViewController:cityVC];
                [self presentModalViewController:naviVC animated:YES];
            }else{
                NSString *mailBox = [[NSUserDefaults standardUserDefaults]objectForKey:kUserMailBox];
                if (mailBox) {
                    ZJZXContentViewController *zjzxContentVC = [[ZJZXContentViewController alloc]init];
                    [self.navigationController pushViewController:zjzxContentVC animated:YES];
                }else{
                    ZJZXMailViewController *zjzxMailVC = [[ZJZXMailViewController alloc]init];
                    [self.navigationController pushViewController:zjzxMailVC animated:YES];
                }
            }
            break;
        }
        default:
            break;
    }
    self.hidesBottomBarWhenPushed = NO;
}


- (void) pushMailViewController{
    self.hidesBottomBarWhenPushed = YES;
    ZJZXMailViewController *mailVC = [[ZJZXMailViewController alloc]init];
    [self.navigationController pushViewController:mailVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
